package cn.iocoder.yudao.module.fengdong.smartelderly.service.activityrecord;

import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.activity.ActivityDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.activity.ActivityMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.activityrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.activityrecord.ActivityRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.activityrecord.ActivityRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.activityrecord.ActivityRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 活动参加记录 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class ActivityRecordServiceImpl implements ActivityRecordService {

    @Resource
    private ActivityRecordMapper activityRecordMapper;
    @Resource
    private ActivityMapper activityMapper;
    @Override
    public Long createActivityRecord(ActivityRecordCreateReqVO createReqVO) {
        // 插入
        ActivityRecordDO activityRecord = ActivityRecordConvert.INSTANCE.convert(createReqVO);
        activityRecordMapper.insert(activityRecord);
        //参与人数+1
        ActivityDO activityDO = activityMapper.selectById(createReqVO.getActivityId());
        activityDO.setNowNum(activityDO.getNowNum()+1);
        activityMapper.updateById(activityDO);

        // 返回
        return activityRecord.getId();
    }

    @Override
    public void updateActivityRecord(ActivityRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateActivityRecordExists(updateReqVO.getId());
        // 更新
        ActivityRecordDO updateObj = ActivityRecordConvert.INSTANCE.convert(updateReqVO);
        activityRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityRecord(Long id) {
        // 校验存在
        ActivityRecordDO activityRecordDO = activityRecordMapper.selectById(id);
        if (activityRecordDO == null) {
            throw exception(ACTIVITY_RECORD_NOT_EXISTS);
        }
        //参与人数-1
        ActivityDO activityDO = activityMapper.selectById(activityRecordDO.getActivityId());
        activityDO.setNowNum(activityDO.getNowNum()-1);
        activityMapper.updateById(activityDO);

        // 删除
        activityRecordMapper.deleteById(id);
    }

    private void validateActivityRecordExists(Long id) {
        if (activityRecordMapper.selectById(id) == null) {
            throw exception(ACTIVITY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ActivityRecordDO getActivityRecord(Long id) {
        return activityRecordMapper.selectById(id);
    }

    @Override
    public List<ActivityRecordDO> getActivityRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return activityRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ActivityRecordDO> getActivityRecordPage(ActivityRecordPageReqVO pageReqVO) {
        return activityRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ActivityRecordDO> getActivityRecordList(ActivityRecordExportReqVO exportReqVO) {
        return activityRecordMapper.selectList(exportReqVO);
    }

    @Override
    public PageResult<ActivityRecordUniteRespVO> getActivityRecordUnitePage(ActivityRecordUnitePageReqVO pageVO) {
        IPage<ActivityRecordUniteRespVO> respVOIPagea=activityRecordMapper.selectActivityRecordUnite(MyBatisUtils.buildPage(pageVO),pageVO,getLoginUserId());
        return ActivityRecordConvert.INSTANCE.convertUnitePage(respVOIPagea);
    }

}
